<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2019-01-14
 * Time: 15:46
 */

namespace Biz\module\Classes;

use Biz\model\trade\OrderRefund as mOrderRefund;
use Biz\model\trade\Order as mOrder;


class OrderRefundInfo
{
    const DEFAULT_REFUND_INFO = [
        'status' => 0,
        'status_desc' => '退款',
        'refund_order_no' => '',
    ];

    public static function getRefundStatusInfo($orderStatus, $refundOrderNos,$from='detail')
    {
        $refundInfoList = [];
        $refundOrderList = mOrderRefund::getRefundOrderList(array_values($refundOrderNos));

        if (empty($refundOrderList) && $from=='detail') {
                return (function() use($refundOrderNos,&$refundInfoList){
                    foreach ($refundOrderNos as $key=>$value){
                        $refundInfoList[$key] = self::DEFAULT_REFUND_INFO;
                    }

                    return $refundInfoList;
                })();
        }

        foreach ($refundOrderNos as $skuId=>$refundOrderNo) {
            $refundOrder = isset($refundOrderList[$refundOrderNo])?$refundOrderList[$refundOrderNo]:[];
            if (!empty($refundOrder)) {
                if ($orderStatus != mOrder::STATUS_CLOSE &&  $from=='detail') {
                    $refundInfoList[$skuId] = OrderRefundInfo::DEFAULT_REFUND_INFO;
                }
                if (!in_array($refundOrder['status'], [mOrderRefund::STATUS_CANCEL, mOrderRefund::STATUS_RETURN_GOODS_CANCEL])) {

                    $refundInfoList[$skuId] = [
                        'status' => $refundOrder['v_app_status'],
                        'status_desc' => $refundOrder['v_app_status_desc'],
                        'refund_order_no' => $refundOrderNo,
                    ];


                }
            }else{
                if($from=='detail'){
                    $refundInfoList[$skuId] = OrderRefundInfo::DEFAULT_REFUND_INFO;
                }
            }
        }


        return $refundInfoList;
    }
}